package ui.magic.magicui.container.LittleThings;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import ui.magic.magicui.container.Frame.FrameMagicUi;

/**
 * Created by liushiyin on 2017/10/28.
 */

public class SlideSlipItem extends FrameMagicUi{

    private View preView=null;
    private View backView=null;
    private ViewDragHelper viewDragHelper;

    public SlideSlipItem(Context context) {
        super(context);
        init();
    }

    public SlideSlipItem(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SlideSlipItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        preView=new TextView(getContext());
        backView=new Button(getContext());
        preView.setBackgroundColor(Color.RED);
        backView.setBackgroundColor(Color.GREEN);
        ((TextView)preView).setGravity(Gravity.CENTER);
        ((TextView)preView).setText("我是前页");
        ((Button)backView).setGravity(Gravity.CENTER);
        ((Button)backView).setText("我是背页");
        this.addChildView(backView,80,100);
        this.addChildView(preView,100,100);
        viewDragHelper=ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                return preView==child;
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                if(child==preView){
                    if(left>0){
                        return 0;
                    }else{
                        return Math.min(left,backView.getMeasuredWidth());
                    }
                }
                return 0;
            }

            @Override
            public int getViewHorizontalDragRange(View child) {
                if(preView==child){
                    return backView.getMeasuredWidth();
                }
                return super.getViewHorizontalDragRange(child);
            }

            @Override
            public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
                if(changedView==preView){
                    backView.offsetLeftAndRight(dx);
                }
                invalidate();
            }

            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                if(releasedChild==preView){
                    if(xvel<0&&Math.abs(preView.getLeft())>backView.getMeasuredWidth()/3.0f){
                        open();
                    } else{
                        close();
                    }
                }
            }
        });
    }

    public void setPreViewAndBackView(View preView,View backView){
        if(preView!=null&&backView!=null){
            this.removeChildView(preView);
            this.removeChildView(backView);
        }
        this.preView=preView;
        this.backView=backView;
    }

    public View getPreView(){
        return preView;
    }

    public View getBackView(){
        return backView;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewDragHelper.processTouchEvent(event);
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action=ev.getAction();
        if(action==MotionEvent.ACTION_CANCEL){
            viewDragHelper.cancel();
            return false;
        }
        return viewDragHelper.shouldInterceptTouchEvent(ev);
    }

    public void open(){
        backView.layout(preView.getMeasuredWidth() - backView.getMeasuredWidth(), 0,preView.getMeasuredWidth(),
                preView.getMeasuredHeight());
        preView.layout(-backView.getMeasuredWidth(), 0, preView.getMeasuredWidth() - backView.getMeasuredWidth(),
                preView.getMeasuredHeight());
    }

    public void close(){
        viewDragHelper.smoothSlideViewTo(preView,0,0);
        backView.layout(preView.getMeasuredWidth(), 0, preView.getMeasuredWidth() + backView.getMeasuredWidth(),
                backView.getMeasuredHeight());
        preView.layout(0, 0, preView.getMeasuredWidth(),preView.getMeasuredWidth());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        close();
    }
}
